package com.issac.vhr.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.VerifyCodeUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class VerifyCodeController {
    /*
    *
    * 将生成的验证码字符保存到 session 中，然后通过流将图片写到前端
    * */
    @GetMapping("/verifyCode")
    public void code(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        VerifyCodeUtils code = new VerifyCodeUtils();
        BufferedImage image = code.getImage();
        String text = code.getText();
        HttpSession session = req.getSession();
        session.setAttribute("verify_code",text);
        VerifyCodeUtils.output(image,resp.getOutputStream());
    }
}
